#ifndef ODOWNLOADERTASK_H
#define ODOWNLOADERTASK_H
#include <OType.h>

#include <curl/curl.h> 
#include <curl/types.h>  
#include <curl/easy.h>

class ODownloaderTask{
	public:
		typedef enum{
			e_task_ready,
			e_task_downloading,
			e_task_userstop,
			e_task_completed,
			e_task_paused,
		}e_taskstate;
		
		ODownloaderTask();
		ODownloaderTask(u16,string,string);

		~ODownloaderTask();

		void Start();
		void Pause();
		void Stop();

		void SetID(int);
		void SetUrl(string);
		void SetPath(string);

		e_taskstate GetTaskState();

		u16 GetID();
		const char *GetUrl();
		const char *GetPath();

		double GetDownloadSpeed();
		double GetUploadSpeed();
		double GetFileSize();
		double GetDownloadSize();
		double GetDownloadProgress();
		
		void AddProperties(char *);
		void ClearProperties();
		void SetPostData(char *);

		friend void *DoDownLoad(void *a);
		
	private:
		u16		ID;
		string	url;
		string	path;
		e_taskstate state;

		pthread_t pid;
		pthread_mutex_t mutex; 
		
		CURL	*curl_handle;
		FILE	*fp;
		struct curl_slist *pAdditionalProperties;
		char *PostData;
		
		void OpenFile();
		void CloseFile();
		void Completed();

		static void *DoDownLoad(void *a);
		static size_t DownLoadWriteFile(void *buffer, size_t size, size_t nmemb, void *user_p);
};
#endif
